﻿package mortal.game.view.copy.sunbathCopy
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.utils.pools.*;
    import extend.language.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.fight.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.*;
    import mortal.game.view.copy.sunbathCopy.view.*;
    import mortal.mvc.core.*;

    public class SunbathCopyController extends CopyControllerBase
    {
        private var _preCkTime:int = 0;
        private var _openTime:int = -1;
        private var _timeout:int;

        public function SunbathCopyController()
        {
            return;
        }// end function

        private function activesNoticHandler(param1:Object) : void
        {
            var _loc_2:* = param1 as int;
            if (this.checkLevel())
            {
                SunbathCopyIcon.instance.show();
                SunbathCopyIcon.instance.updateOpenTime(_loc_2, 1);
            }
            else if (SunbathCopyIcon.hasInstance)
            {
                SunbathCopyIcon.instance.hide();
            }
            return;
        }// end function

        private function onSunbathCopyEnterViewShow(event:DataEvent) : void
        {
            var _loc_2:* = CopyConfig.instance.getCopyInfoByCode(130);
            if (_loc_2.enterMinLevel > cache.role.entityInfo.level)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(70977));
                return;
            }
            SunbathCopyEnterView.instance.show();
            return;
        }// end function

        private function onSunbathInfoReq(event:DataEvent) : void
        {
            GameProxy.copy.getSunbathInfo();
            return;
        }// end function

        private function onSunbathCopyEnter(event:DataEvent) : void
        {
            CopyUtil.copyCheck(this.enterAfterCheck, event);
            return;
        }// end function

        private function enterAfterCheck(event:DataEvent) : void
        {
            GameProxy.copy.enterSunbath(event.data as int);
            return;
        }// end function

        private function onSunbathCopyOper(event:DataEvent) : void
        {
            if (!RolePlayer.instance.isInSpasticity)
            {
                GameProxy.copy.sunbathOper(event.data.type as int, event.data.sEntityId as SEntityId);
            }
            return;
        }// end function

        private function onSunbathCopyOperNew(event:DataEvent) : void
        {
            if (!this.canDoAction(event.data))
            {
                return;
            }
            var _loc_2:* = getTimer();
            if (_loc_2 - this._preCkTime < 1000)
            {
                return;
            }
            this._preCkTime = _loc_2;
            SunbathUtil.oper(event.data);
            return;
        }// end function

        private function canDoAction(param1:Object) : Boolean
        {
            var _loc_2:String = null;
            if (RolePlayer.instance.isInSwinArea)
            {
                _loc_2 = "";
                switch(param1["type"])
                {
                    case ESunbathOper._ESunbathOperKiss:
                    {
                        _loc_2 = "" + Language.getString(70978);
                        break;
                    }
                    case ESunbathOper._ESunbathOperShoot:
                    {
                        _loc_2 = "" + Language.getString(70979);
                        break;
                    }
                    case ESunbathOper._ESunbathOperPerform:
                    {
                        _loc_2 = "" + Language.getString(70980);
                        break;
                    }
                    case ESunbathOper._ESunbathOperDance:
                    {
                        _loc_2 = "" + Language.getString(70981);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                MsgManager.showRollTipsMsg(Language.getStringByParam(70982, _loc_2));
                return false;
            }
            return true;
        }// end function

        private function onDanceListBack(event:DataEvent) : void
        {
            var _loc_2:* = SunbathUtil.getDanceList();
            SunbathDanceListPanel.instance.show();
            SunbathDanceListPanel.instance.updateList(_loc_2);
            return;
        }// end function

        private function initHd(param1:Object) : void
        {
            if (this._openTime != -1 && this.checkLevel())
            {
                SunbathCopyIcon.instance.show();
            }
            return;
        }// end function

        private function onSunBathCopyOpen(param1:Object) : void
        {
            this._openTime = param1 as int;
            if (this.checkLevel() == false)
            {
                return;
            }
            SunbathCopyIcon.instance.show();
            SunbathCopyIcon.instance.updateOpenTime(param1 as int, 2);
            var _loc_2:* = CopyConfig.instance.getCopyInfoByCode(130);
            if (_loc_2.enterMinLevel <= cache.role.entityInfo.level)
            {
                SunbathCopyEnterView.instance.show();
            }
            return;
        }// end function

        private function onSunBathCopyClose(param1:Object) : void
        {
            this._openTime = -1;
            if (SunbathCopyIcon.hasInstance && !SunbathCopyIcon.instance.isHide)
            {
                SunbathCopyIcon.instance.hide();
            }
            if (SunbathCopyEnterView.hasInstance && SunbathCopyEnterView.instance.isHide == false)
            {
                SunbathCopyEnterView.instance.hide();
            }
            if (SunbathDanceListPanel.hasInstance && SunbathDanceListPanel.instance.isHide == false)
            {
                SunbathDanceListPanel.instance.hide();
            }
            return;
        }// end function

        private function onSunbathOper(param1:SSunBathMsg) : void
        {
            var _loc_4:int = 0;
            if (param1 == null)
            {
                return;
            }
            var _loc_2:* = ThingUtil.entityUtil.getEntity(param1.fromEntityId) as UserPlayer;
            var _loc_3:* = ThingUtil.entityUtil.getEntity(param1.toEntityId) as UserPlayer;
            if (_loc_2 == null || _loc_3 == null)
            {
                return;
            }
            switch(param1.operType)
            {
                case ESunbathOper._ESunbathOperShoot:
                {
                    _loc_4 = 51300001;
                    _loc_2.entityInfo.updateShootWeapon();
                    if (_loc_2 && _loc_2 is RolePlayer)
                    {
                        RolePlayer.instance.spasticityBeginFight(1400);
                    }
                    SunbathSpeakUtil.speakByType(SunbathSpeakUtil.shoot, param1.fromEntityId);
                    GameManager.instance.updateLastOperTime();
                    break;
                }
                case ESunbathOper._ESunbathOperKiss:
                {
                    _loc_4 = 51300002;
                    SunbathSpeakUtil.speakByType(SunbathSpeakUtil.kiss, param1.fromEntityId);
                    GameManager.instance.updateLastOperTime();
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            if (_loc_4)
            {
                this.fireSunbathSkill(_loc_2, _loc_3, _loc_4);
            }
            return;
        }// end function

        private function fireSunbathSkill(param1:UserPlayer, param2:UserPlayer, param3:int) : void
        {
            var _loc_5:FightEffectNormal = null;
            if (!param1 || !param2 || !param3)
            {
                return;
            }
            var _loc_4:* = SkillConfig.instance.getInfoById(param3);
            if (!_loc_4)
            {
                return;
            }
            _loc_5 = ObjectPool.getObject(FightEffectNormal);
            _loc_5.setFromPlayer(param1);
            _loc_5.setSkill(_loc_4);
            _loc_5.isNotAutoShield = true;
            _loc_5.setTargetPlayer(param2);
            _loc_5.setHitPlayers(this.Vector.<SpritePlayer>([param2]));
            _loc_5.runStart();
            return;
        }// end function

        private function onPlayerNumChange(param1:Object) : void
        {
            if (SunbathCopySkillBarNew.hasInstance() && SunbathCopySkillBarNew.instance.isHide == false)
            {
                SunbathCopySkillBarNew.instance.updateNum();
            }
            return;
        }// end function

        private function checkLevel() : Boolean
        {
            var _loc_1:* = Cache.instance.role.entityInfo.level;
            var _loc_2:* = GameDefConfig.instance.getSystemOpenLevel(55);
            if (_loc_1 >= _loc_2)
            {
                return true;
            }
            return false;
        }// end function

        override public function enterCopy(param1:TCopy) : void
        {
            super.enterCopy(param1);
            SunbathUtil.isInSunbatch = true;
            Cache.instance.copy.sunbath.exp = 0;
            Cache.instance.copy.sunbath.vitalEnergy = 0;
            SunbathCopyEnterView.instance.hide();
            SunbathCopyInfoView.instance.show();
            SunbathCopySkillBarNew.instance.show();
            return;
        }// end function

        override public function leaveCopy() : void
        {
            super.leaveCopy();
            SunbathUtil.isInSunbatch = false;
            SunbathCopyInfoView.instance.hide();
            SunbathCopySkillBarNew.instance.hide();
            SunBathPassWin.instance.show();
            RolePlayer.instance.updatePlayMusical(false);
            RolePlayer.instance.entityInfo.isInPlayMusical = false;
            return;
        }// end function

        private function onCloseDanceListHd(param1:Object) : void
        {
            SunbathDanceListPanel.instance.hide();
            return;
        }// end function

        override protected function initServer() : void
        {
            EventUtil.addNetEvent(ServerCommand.SunBathCopyBeginOpen, this.activesNoticHandler, true);
            Dispatcher.addEventListener(EventName.SunbathCopyEnterViewShow, this.onSunbathCopyEnterViewShow);
            Dispatcher.addEventListener(EventName.SunbathInfoReq, this.onSunbathInfoReq);
            Dispatcher.addEventListener(EventName.SunbathCopyEnter, this.onSunbathCopyEnter);
            NetDispatcher.addCmdListener(ServerCommand.SunBathCopyOpen, this.onSunBathCopyOpen);
            NetDispatcher.addCmdListener(ServerCommand.SunBathCopyClose, this.onSunBathCopyClose);
            NetDispatcher.addCmdListener(ServerCommand.SunBathOperRes, this.onSunbathOper);
            EventUtil.addNetEvent(ServerCommand.SunbathPlayerInfoRes, this.onPlayerNumChange, true);
            EventUtil.addSelfEvent(EventName.NavbarButtonUIRowInited, this.initHd, true);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.initHd);
            return;
        }// end function

        override protected function addListeners() : void
        {
            Dispatcher.addEventListener(EventName.SunbathCopyOper, this.onSunbathCopyOper);
            this.addEvs(true);
            return;
        }// end function

        override protected function removeListeners() : void
        {
            Dispatcher.removeEventListener(EventName.SunbathCopyOper, this.onSunbathCopyOper);
            this.addEvs(false);
            return;
        }// end function

        private function addEvs(param1:Boolean) : void
        {
            EventUtil.addSelfEvent(EventName.SunbathCopyOperNew, this.onSunbathCopyOperNew, param1);
            EventUtil.addNetEvent(ServerCommand.SunbathInfoDance, this.onDanceListBack, param1);
            EventUtil.addSelfEvent(EventName.SunbathCopyCloseDanceList, this.onCloseDanceListHd, param1);
            return;
        }// end function

    }
}
